package com.innerchic.mapper.tool;

import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;

/**
 * ================================================
 * 作    者：贾恒飞 >>> 17515250730@163.com
 * 项    目：young
 * 日    期：2021/11/4 10:14
 * 包    名：com.innerchic.mapper.tool
 * 描    述：动画工具类
 * Create by zFox from AndroidStudio4.2
 * ================================================
 */
public class AnimTool {

    /**
     * 渐变出现
     */
    public static void fadeIn(View view, float startAlpha, float endAlpha, long duration) {
        if (view.getVisibility() == View.VISIBLE) return;
        view.setVisibility(View.VISIBLE);
        Animation animation = new AlphaAnimation(startAlpha, endAlpha);
        animation.setDuration(duration);
        view.startAnimation(animation);
    }

    /**
     * 渐变出现
     *
     * @param view View
     */
    public static void fadeIn(View view) {
        fadeIn(view, 0F, 1F, 400);
        // We disabled the button in fadeOut(), so enable it here.
        view.setEnabled(true);
    }

    /**
     * 渐变退出
     *
     * @param view View
     */
    public static void fadeOut(View view) {
        if (view.getVisibility() != View.VISIBLE) return;
        fadeOut(view, 400);
    }

    public static void fadeOut(View view, long durationMillis) {
        if (view.getVisibility() != View.VISIBLE) return;
        // Since the button is still clickable before fade-out animation
        // ends, we disable the button first to block click.
        view.setEnabled(false);
        Animation animation = new AlphaAnimation(1F, 0F);
        animation.setDuration(400);
        view.startAnimation(animation);
        view.setVisibility(View.GONE);
    }

    /**
     * 获取上下动的动画
     *
     * @param view 图像
     * @param p    偏移量
     * @return 动画对象, 可以停止
     */
    public static TranslateAnimation translateAnimation(View view, int p) {
        int[] location = new int[2];
        view.getLocationOnScreen(location);//获取视图位置
        int x = location[0];
        int y = location[1];
        TranslateAnimation tAnim = new TranslateAnimation(x, x, y, y + p);//设置视图上下移动的位置
        tAnim.setDuration(1000);
        tAnim.setRepeatCount(Animation.INFINITE);
        tAnim.setRepeatMode(Animation.REVERSE);
        view.setAnimation(tAnim);
        tAnim.start();
        return tAnim;
    }

}
